<?php

namespace app\controllers\admin\uni\role;

use app\controllers\admin\base\AdminController as BaseController;
use app\models\Admin as Model;
use app\models\role\AdminRole;
use app\models\role\Permission;
use app\models\role\Role;
use yii\db\Exception;

class AdminController extends BaseController
{
    public function actionList()
    {
        $page = intval($this->post('page', $this->page));
        $page_size = intval($this->post('pageSize', $this->pageSize));

        $query = Model::find();

        $totalCount = $query->count();
        $totalPage = ceil($totalCount / $page_size);

        $list = $query->offset(($page - 1) * $page_size)
            ->limit($page_size)
            ->asArray()
            ->all();

        // 权限
        foreach ($list as &$item) {
            // $item['permission'] = Permission::find()
            //     ->where(['']);
        }

        return $this->success([
            $this->totalCount   => $totalCount,
            $this->totalPage    => $totalPage,
            $this->list         => $list,
        ]);

    }

    public function actionEdit()
    {
        $data = $this->post();

        try {
            if (isset($data['id']) && $data['id'] > 0) {
                $model = Model::findOne(['id' => $data['id']]);
                if (!$model) {
                    return $this->fail('操作失败');
                }
            } else {
                $model = new Model();
                $model->password = md5('123456' . \Yii::$app->params['ADMIN_SALT']);
            }
            $data = ['Admin' => $data];
            if ($model->load($data) && $model->save()) {
                return $this->success($model, '操作成功');
            } else {
                throw new Exception($model->getFirstError('username'));
            }
        } catch (\Exception $e) {
            return $this->fail($e->getMessage());
        }
    }

    public function actionView()
    {
        $id = $this->post('id');
        $model = Model::find()
            ->where(['id' => $id])
            ->asArray()
            ->one();

        if ($model) {
            return $this->success($model);
        }

        return $this->fail('不存在该角色');
    }

    public function actionRemove()
    {
        $id = $this->post('id');

        try {
            $model = Model::findOne(['id' => $id]);
            if ($model->can_delete != '1') {
                return $this->fail('超级权限(角色)不能删除!');
            }

            if ($model->delete()) {
                return $this->success($model, '删除成功');
            }
        } catch (\Exception $e) {
            return $this->fail($e->getMessage());
        }
    }

    // role list
    public function actionRoleList()
    {
        $admin_id = $this->post('id');

        // admin
        $admin = Model::findOne(['id' => $admin_id]);

        if (!$admin) {
            return $this->fail();
        }

        // role list
        $list = Role::find()
            ->orderBy(['id' => SORT_ASC])
            ->asArray()
            ->all();

        // checked role
        $selected = AdminRole::find()
            ->where(['admin_id' => $admin_id])
            ->orderBy(['id' => SORT_ASC])
            ->select(['role_id'])
            ->column();

        return $this->success([
            'list' => $list,
            'admin' => $admin,
            'selected' => $selected
        ]);
    }

    // save role
    public function actionRoleSave()
    {
        $admin_id = $this->post('id');
        $selected = $this->post('selected');

        if (empty($selected)) {
            return $this->fail('请勾选~~~');
        }

        // 先删除, 再增加(这里懒得做对比了)
        AdminRole::deleteAll(['admin_id' => $admin_id]);

        foreach ($selected as $v) {
            $model = new AdminRole();
            $model->admin_id = $admin_id;
            $model->role_id = $v;
            $model->save();
        }

        return  $this->success();
    }
}